package com.sinodata.bsm.cicp.finder.tomcat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.JmxUtil;

public class ThreadPoolFinder extends Finder {

    private static final Logger logger = Logger.getLogger(ThreadPoolFinder.class);

    /* @Override
     public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
         List resources = new ArrayList();
         String ip = host;
         String port = attrVals[4].getAttrValue();
         String name = null;
         JMXConnector connector = null;
         MBeanServerConnection connection = null;
         try {
             connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
             connection = connector.getMBeanServerConnection();
             ObjectName tomcatThreadPool = new ObjectName("Catalina:type=ThreadPool,*");
             Set<ObjectName> set = connection.queryNames(tomcatThreadPool, null);
             for (ObjectName objectName : set) {
                 name = objectName.getKeyProperty("name");
                 List oneResource = CreateOneResource(name, attributes);
                 resources.add(oneResource);
             }
         } catch (Exception e) {
             logger.error("failed to find tomcat ThreadPool" + e);
         } finally {
             JmxUtil.closeConnector(connector);
         }
         return resources;
     }*/

    /*private List CreateOneResource(String name, ResAttributeValue[] attributes) {
        List aRes = new ArrayList();
        ResValue res = new ResValue();
        ResAttributeValValue valValue = null;
        res.setIp(host); // host IP address
        res.setInternalId(host + "_tomcatManager_" + name); // Internal ID for
        res.setName("ThreadPool." + name);
        aRes.add(res);
        valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), name);
        aRes.add(valValue);
        return aRes;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String port = (String) params.get("PORT");
        String name = null;
        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            //connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            ObjectName tomcatThreadPool = new ObjectName("Catalina:type=ThreadPool,*");
            Set<ObjectName> set = connection.queryNames(tomcatThreadPool, null);
            for (ObjectName objectName : set) {
                name = objectName.getKeyProperty("name");
                String[] info = new String[4];
                list.add(info);
                info[0] = ip + "_tomcatManager_" + name;
                info[1] = "Cache." + name;
                info[2] = "Cache " + name;
                info[3] = name; //名称
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find tomcat ThreadPool" + e);
            return null;
        } finally {
            JmxUtil.closeConnector(connector);
        }
    }
}
